using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Coin : MonoBehaviour
{
    public float area;
    public float speed;
    public LayerMask layerPlayer;
    public bool iscoin;
    private Collider2D coll;
    // Start is called before the first frame update
    void Start()
    {
        coll = GetComponent<Collider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Physics2D.OverlapCircle(transform.position, area, layerPlayer))
        {
            Invoke("Move", .5f);
        }
    }
    void Move()
    {
        Vector2 trans = GameObject.FindWithTag("Player").transform.position;
        transform.position = Vector2.MoveTowards(transform.position, trans, speed * Time.deltaTime);
    }
    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(transform.position, area);
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            if (iscoin)
            { collision.GetComponent<PlayerCon>().parameterPlayer.coin++; }
            else
            { collision.GetComponent<PlayerCon>().parameterPlayer.NowMP += 8; }
            coll.enabled = false;
            Destroy(gameObject,.3f);
        }
    }
}
